package com.ruoyi.binding.controller;

import com.ruoyi.binding.service.impl.DispatchTaskService;
import com.ruoyi.common.core.domain.R;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/binding/dispatch/task")
public class DispatchTaskController {

    @Autowired
    private DispatchTaskService dispatchTaskService;

    @PostMapping("/start/{taskId}")
    public R<Void> startTask(@PathVariable Long taskId) {
        try {
            dispatchTaskService.startTask(taskId);
            return R.ok(null, "任务已开始");
        } catch (SchedulerException e) {
            log.error("启动任务失败", e);
            return R.fail("启动任务失败: " + e.getMessage());
        }
    }

    @PostMapping("/stop/{taskId}")
    public R<Void> stopTask(@PathVariable Long taskId) {
        try {
            dispatchTaskService.stopTask(taskId);
            return R.ok(null, "任务已停止");
        } catch (SchedulerException e) {
            log.error("停止任务失败", e);
            return R.fail("停止任务失败: " + e.getMessage());
        }
    }
}
